<?php

require_once __DIR__ . '/../../../../vendor/autoload.php';

use JLPay\Core\OrgConfig;
use JLPay\Core\DemoConfig;
use JLPay\Services\Trans\Mini\Alipay\WapPayService;
use JLPay\Models\Trans\Mini\Alipay\WapPayRequest;
use JLPay\Models\Trans\Mini\Alipay\WapPayResponse;
use JLPay\Utils\Logger;
use JLPay\Utils\DemoRequestHandler;

/**
 * 交易 - Mini支付宝WAP支付功能使用示例
 */

// =================== 工具函数 =====================
/**
 * 创建配置对象
 */
function createConfig(): OrgConfig
{
    return DemoConfig::createConfig();
}

/**
 * 构建支付宝WAP支付请求
 */
function buildWapPayRequest(): WapPayRequest
{
    $request = new WapPayRequest();
    
    // 设置请求参数
    $request->setMchId("849584358120018")
        ->setTermNo("60131234")
        ->setPayType("alipay")
        ->setBuyerLogonId("test@alipay.com")
        ->setBuyerId("2088102122458832")
        ->setLimitPay("no_credit")
        ->setOutTradeNo("TEST_WAP_PAY_001")
        ->setPaymentValidTime("20241231235959")
        ->setBody("测试支付宝WAP支付商品")
        ->setAttach("测试支付宝WAP支付描述")
        ->setTotalFee("200")
        ->setIsHirePurchase("0")
        ->setHirePurchaseNum("3")
        ->setHirePurchaseSellerPercent("0")
        ->setRemark("测试支付宝WAP支付备注")
        ->setNotifyUrl("https://example.com/notify")
        ->setOpUserId("test_user")
        ->setOpShopId("GD_SZ_001")
        ->setDeviceInfo("N50007A8890")
        ->setMchCreateIp("127.0.0.1")
        ->setLongitude("116.397128")
        ->setLatitude("39.916527");
    
    return $request;
}

// =================== 示例函数 =====================
/**
 * 支付宝WAP支付示例
 */
function wapPayDemo(): void
{
    $logger = Logger::getInstance();
    $logger->info("开始执行支付宝WAP支付示例");
    
    // 创建配置对象和支付宝WAP支付服务
    $config = createConfig();
    $wapPayService = new WapPayService($config);
    
    // 创建支付宝WAP支付请求
    $request = buildWapPayRequest();
    
    // 发送请求 - 使用DemoRequestHandler
    $response = DemoRequestHandler::sendRequestAndHandleResponse(
        $wapPayService, 
        $request, 
        "支付宝WAP支付",
        function($service, $request) {
            return $service->wapPay($request);
        }
    );
    
    // 处理响应结果
    if ($response && $response->isSuccess()) {
        $logger->info("响应报文：" . json_encode($response->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    }
    
    $logger->info("支付宝WAP支付示例执行完毕");
}

// =================== 主程序 =====================
$logger = Logger::getInstance();

$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 支付宝WAP支付示例 开始 =====");
$logger->info("=" . str_repeat("=", 50));

wapPayDemo();

$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 支付宝WAP支付示例执行完毕 =====");
$logger->info("=" . str_repeat("=", 50)); 